<cfcomponent displayname="ExamService" hint="CFC to provide Exam Service" output="false">
	<!------------------------------------------- CONSTRUCTOR ------------------------------------------->
	<cffunction name="init" access="public" returntype="model.ExamService" output="false">
		<cfargument name="oTransfer" required="yes" type="any" hint="Transfer Factory Object">
		<cfargument name="examID" required="false" type="numeric" hint="ID of an exam" default="0">
		
		<cfif NOT isDefined( "variables.oTransfer" )>
			<cfset variables.oTransfer = Arguments.oTransfer>
		</cfif>
		
		<cfif Arguments.examID gt 0>
			<cfset variables.oExamBean 
					= variables.oTransfer.get("exam.exam", Val(Arguments.examID))>			
		</cfif>
		<cfreturn this>
	</cffunction>
	<!------------------------------------------- PUBLIC ------------------------------------------->

	<cffunction name="getExam" access="public" returntype="any">
		<cfargument name="examID" required="yes" type="numeric" hint="id of the exam">
		
		<!--- TODO: AOP to check whether objTransfer exists --->
		<cfscript>
			var oExam = variables.oTransfer.get("exam.exam", Val(Arguments.examID));
			//getPlugin("beanFactory").populateBean( ); if we are doing edit
		</cfscript>
		<cfreturn oExam>
	</cffunction>

	<cffunction name="setExam" access="public" returntype="any">
		<cfargument name="examID" required="yes" type="numeric" hint="id of the exam">
		<!--- TODO: AOP to check whether objTransfer exists --->
		<cfscript>
			var oExam = variables.oTransfer.get("exam.exam", Val(Arguments.examID));
			var oEventHandler = createObject("component","coldbox.system.eventhandler");
			oEventHandler.getPlugin("beanFactory").populateBean( oExam ); 
		</cfscript>
		<cfreturn oExam>
	</cffunction>

	<cffunction name="saveExam" access="public" returntype="transfer.com.TransferObject">
		<cfargument name="examBean" required="yes" type="transfer.com.TransferObject" hint="Exam bean">
		<cfscript>
			var userBean = variables.oTransfer.get( "user.user" , SESSION.user_id );
			variables.oTransfer.save( Arguments.examBean );
			userBean.addUserExams( examBean );
			variables.oTransfer.save( userBean );
		</cfscript>
		<cfreturn examBean>
	</cffunction>
	
	<cffunction name="getExams" access="public" returntype="struct">
		<!--- TODO: AOP to check whether objTransfer exists --->
		<cfargument name="userID" required="yes" type="numeric" hint="id of the exam owner">
		<cfargument name="returnType" required="no" type="string" 
					hint="return type can be query or transfer" default="transfer">
		<cfset var userBean = "">
	 	<cfset userBean = variables.oTransfer.get( "user.user" , Val(Arguments.userID) )>
		<cfreturn userBean.getUserExamsStruct()>
		<cfreturn structNew()>
	</cffunction>

	<cffunction name="getQuestions" access="public" returntype="struct" output="true">
	 	<cfreturn variables.oExamBean.getQuestionStruct()>
	</cffunction>

	<cffunction name="getQuestion" access="public" returntype="transfer.com.TransferObject">
		<cfargument name="questionID" required="yes" type="numeric" hint="id of the question">
		<cfreturn variables.oTransfer.get( "exam.question" , Val(Arguments.questionID))>
	</cffunction>


</cfcomponent>